package cn.iocoder.yudao.module.english.controller.admin.level;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.english.controller.admin.level.vo.LevelManagementPageReqVO;
import cn.iocoder.yudao.module.english.controller.admin.level.vo.LevelManagementRespVO;
import cn.iocoder.yudao.module.english.controller.admin.level.vo.LevelManagementSaveReqVO;
import cn.iocoder.yudao.module.english.dal.dataobject.level.LevelManagementDO;
import cn.iocoder.yudao.module.english.service.level.LevelManagementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 级别管理")
@RestController
@RequestMapping("/english/level-management")
@Validated
public class LevelManagementController {

    @Resource
    private LevelManagementService levelManagementService;

    @PostMapping("/create")
    @Operation(summary = "创建级别管理")
    @PreAuthorize("@ss.hasPermission('english:level-management:create')")
    public CommonResult<Long> createLevelManagement(@Valid @RequestBody LevelManagementSaveReqVO createReqVO) {
        return success(levelManagementService.createLevelManagement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新级别管理")
    @PreAuthorize("@ss.hasPermission('english:level-management:update')")
    public CommonResult
            <Boolean> updateLevelManagement(@Valid @RequestBody LevelManagementSaveReqVO updateReqVO) {
        levelManagementService.updateLevelManagement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除级别管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('english:level-management:delete')")
    public CommonResult
            <Boolean> deleteLevelManagement(@RequestParam("id") Long id) {
        levelManagementService.deleteLevelManagement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得级别管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('english:level-management:query')")
    public CommonResult<LevelManagementRespVO> getLevelManagement(@RequestParam("id") Long id) {
        LevelManagementDO levelManagement = levelManagementService.getLevelManagement(id);
        return success(BeanUtils.toBean(levelManagement, LevelManagementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得级别管理分页")
    @PreAuthorize("@ss.hasPermission('english:level-management:query')")
    public CommonResult<PageResult<LevelManagementRespVO>> getLevelManagementPage(@Valid LevelManagementPageReqVO pageReqVO) {
        PageResult<LevelManagementDO> pageResult = levelManagementService.getLevelManagementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LevelManagementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出级别管理 Excel")
    @PreAuthorize("@ss.hasPermission('english:level-management:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLevelManagementExcel(@Valid LevelManagementPageReqVO
                                                   pageReqVO,
                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LevelManagementDO> list = levelManagementService.getLevelManagementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "级别管理.xls",
                "数据", LevelManagementRespVO.class,
                BeanUtils.toBean(list, LevelManagementRespVO.class));
    }

}